package com.wfh.mianshiji.config;

import lombok.Data;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * @Title: RabbitMqConfig
 * @Author wangfenghuan
 * @Package com.wfh.mianshiji.config
 * @Date 2025/11/3 08:58
 * @description: mq配置类
 */
@Configuration
@Component
@Data
public class RabbitMqConfig {

    public static final String EXCHANGE_NAME = "mianshiji";
    public static final String QUEUE_NAME = "mianshiji";
    public static final String ROUTER_KEY = "mianshiji";

    @Value("${spring.rabbitmq.host}")
    private String host;

    @Value("${spring.rabbitmq.password}")
    private String password;

    @Value("${spring.rabbitmq.username}")
    private String username;


    /**
     * 交换机
     *
     * @return
     */
    @Bean
    public DirectExchange exchange() {
        return new DirectExchange(EXCHANGE_NAME);
    }

    /**
     * 队列
     *
     * @return
     */
    @Bean
    public Queue queue() {
        return new Queue(QUEUE_NAME, false);
    }

    /**
     * 绑定队列到交换机
     *
     * @return
     */
    @Bean
    public Binding binding() {
        return BindingBuilder
                .bind(queue())
                .to(exchange())
                .with(ROUTER_KEY);
    }

    /**
     * 装配自定义消息转换器
     *
     * @return
     */
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }


}
